<?php
/**
* 名称：市场资讯模块控制器。
* 描述：市场资讯模块相关的控制响应程式。
*/
final class Controller_News extends Controller{
    /**
    * 描述：列表视图程式。
    * @access protected
    * @return void
    */
    protected function Main(){
        $data=array(
            'base'=>array(
                'newsHot'=>Model_News::GetDataForHot(),  //热门新闻
                'news0002'=>Model_News::GetDataX0002(), //政策法规
                'news0003'=>Model_News::GetDataX0003(), //综合
                'news0004'=>Model_News::GetDataX0004(), //船舶(集)
                'news0005'=>Model_News::GetDataX0005(), //集装箱
                'news0006'=>Model_News::GetDataX0006(), //冶金
                'news0007'=>Model_News::GetDataX0007(), //钢铁
                'news0008'=>Model_News::GetDataX0008(), //煤炭
                'news0009'=>Model_News::GetDataX0009(), //化工
                'news0010'=>Model_News::GetDataX0010(), //石油
                'news0011'=>Model_News::GetDataX0011(), //民生
                'news0012'=>Model_News::GetDataX0012(), //期货
                'news0013'=>Model_News::GetDataX0013(), //国内
                'news0014'=>Model_News::GetDataX0014(), //国际
                'news0017'=>Model_News::GetDataX0017(), //头条
                'news0018'=>Model_News::GetDataX0018(), //要闻
                'news0019'=>Model_News::GetDataX0019(), //大宗资讯
                'news0015'=>Model_News::GetDataX0015(), //类别之宏观
                'news0016'=>Model_News::GetDataX0016(), //类别之产业经济
                //~
                'ad'=>Model_Ad::NewsIndex(),
                'slide'=>Model_Slide::NewsIndex()
            )
        );
        //~
        self::$Template->Assign('data',$data);
        self::$Template->Display('news/index.html');
    }

    /**
    * 描述：列表视图程式。
    * @access protected
    * @return void
    */
    protected function Listview(){
        $arguments=array(
            'type'=>Route::GetArguments('type'),'class'=>(int)Route::GetArguments('class')
        );
        //~
        $scalar=array(
            'table'=>self::$Database->Table('news'),
            'sql'=>$this->Function_Listview_SQL($arguments['type'],$arguments['class'])
        );
        //~
        $data=array(
            'meta'=>array(
                'breadcrumb'=>$this->Function_Listview_Breadcrumb($arguments['type'],$arguments['class'])
            ),
            'base'=>array(
                'list'=>self::$Database->GetLimit($scalar['sql'],Route::GetArguments('page'),20),
                'newsHot'=>Model_News::GetDataForHot()
            ),
            'arguments'=>array_filter($arguments)
        );
        //~
        foreach($data['base']['list']['Rows'] as &$row){
            $purifier=new HTMLPurifier(HTMLPurifier_Config::createDefault());
            $row['summary']=$purifier->purify($row['content']);
        }
        //~
        self::$Template->Assign('data',$data);
        self::$Template->Display('news/list.html');
    }

    /**
    * 描述：详细信息程式。
    * @access protected
    * @return void
    */
    protected function Detail(){
        $arguments=array(
            'id'=>(int)Route::GetArguments('id'),
        );
        //~
        $scalar=array(
            'class'=>self::$Database->GetOne('SELECT class FROM '.self::$Database->Table('news').'WHERE id=?',array($arguments['id']))
        );
        //~
        $data=array(
            'meta'=>array(
                'breadcrumb'=>array(
                    'listName'=>self::$Database->GetOne('SELECT name FROM '.self::$Database->Table('news_class').'WHERE id=?',array($scalar['class'])),
                    'listLink'=>Route::MakeHyperlink(':news::listview',array('class'=>$scalar['class']))
                )
            ),
            'base'=>array(
                'main'=>self::$Database->GetRow(
                    'SELECT * FROM '.self::$Database->Table('news').' WHERE id=?',
                    array($arguments['id'])
                ),
                'previous'=>self::$Database->GetRow(
                    'SELECT * FROM '.self::$Database->Table('news').' WHERE id>? AND class=? ORDER BY datetime ASC',
                    array($arguments['id'],$scalar['class'])
                ),
                'next'=>self::$Database->GetRow(
                    'SELECT * FROM '.self::$Database->Table('news').'  WHERE id<? AND class=? ORDER BY datetime DESC',
                    array($arguments['id'],$scalar['class'])
                ),
                'newsHot'=>Model_News::GetDataForHot(),
                'news0002'=>Model_News::GetDataX0002()
            )
        );
        //~
        if(empty($data['base']['main'])){
            Route::Http404();
            return;
        }
        //~
        self::$Template->Assign('data',$data);
        self::$Template->Display('news/detail.html');
    }

    /**
    * 描述：绑定横幅幻灯片数据。
    * @access protected
    * @return void
    */
    protected function Slide(){
        $data=self::$Database->GetAll('SELECT * FROM '.self::$Database->Table('slide').' WHERE type=\'news-index\' ORDER BY id DESC LIMIT 0,5'); //,7,8
        $xml='<?xml version="1.0" encoding="utf-8"?>';
        $xml.='<bcaster autoPlayTime="3">';
        foreach ($data as $row) {
            $xml.='<item item_url="/data/000/slide/'.$row['picture'].'" link="'.$row['link'].'"></item>';
        }
        $xml.='</bcaster>';
        //~
        header("Content-type:text/xml");
        echo $xml;
    }

    private function Function_Listview_Breadcrumb($type,$class){
        $var=array(
            'listName'=>'最新资讯',
            'listLink'=>Route::MakeHyperlink(':news::listview'),
            'listNameForType'=>array(
                'hot'=>'热点排行'
            )
        );
        //~
        if(empty($type)){
            if($class>0){
                $var['listName']=self::$Database->GetOne('SELECT name FROM '.self::$Database->Table('news_class').' WHERE id='.$class);
                $var['listLink']=Route::MakeHyperlink(':news::listview',array('class'=>$class));
            }
        }
        else{
            if(isset($var['listNameForType'][$type])){
                $var['listName']=$var['listNameForType'][$type];
                $var['listLink']=Route::MakeHyperlink(':news::listview',array('type'=>$type));
            }
        }
        //~
        return array('listName'=>$var['listName'],'listLink'=>$var['listLink']);
    }

    private function Function_Listview_SQL($type,$class){
        switch($type){
            case 'hot':
                return 'SELECT * FROM '.self::$Database->Table('news').' ORDER BY clickCount DESC,id DESC';
            break;
            default:
                if($class>0){
                    return 'SELECT * FROM '.self::$Database->Table('news').' WHERE class='.$class.' ORDER BY datetime DESC';
                }
                else{
                    return 'SELECT * FROM '.self::$Database->Table('news').' ORDER BY datetime DESC';
                }
        }
    }
}